package com.talkweb.newproject.admin.controller;

import com.talkweb.newproject.admin.rest.SysRoleRest;
import com.talkweb.newproject.admin.rest.SysRoleUserRest;
import com.talkweb.newproject.admin.sysrole.dto.SysRoleDTO;
import com.talkweb.newproject.admin.sysroleuser.dto.SysRoleUserDTO;
import com.talkweb.newproject.api.model.admin.SysRoleParam;
import com.talkweb.newproject.api.model.admin.SysRoleUserParam;
import com.talkweb.newproject.api.model.common.Response;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by qsf on 2019/5/14.
 */
@RestController
@RequestMapping(value="/sysorg-api")
@Slf4j
public class SysRoleController {
    @Autowired
    private SysRoleRest sysRoleRest;
    @Autowired
    private SysRoleUserRest sysRoleUserRest;

    @RequestMapping(value="/addRole",method = RequestMethod.POST)
    public ResponseResult<Object> addRole(@RequestBody SysRoleDTO obj) throws Exception {
        ResponseResult<Object> result= new ResponseResult<>();
        try{
            if(obj!= null && obj.getId()>0) {
                if(obj.getValidateState()==null) {
                    obj.setValidateState("1");
                }
                obj.setVersion(1L);
                result = sysRoleRest.update(obj);
            }else {
                obj.setValidateState("1");
                obj.setVersion(1L);
                result = sysRoleRest.create(obj);
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setMessage("执行异常,请重试");
            result.setStatus(Status.FAILD);
            return result;
        }

    }

    /**
     * 根据id查询角色信息
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/getSysRoleByid",method = RequestMethod.POST)
    public ResponseResult<SysRoleDTO> getSysRoleById(@RequestParam("id") String id) throws Exception {
        ResponseResult<SysRoleDTO> result = new ResponseResult<>();
        try{

            return sysRoleRest.querySysRoleByPrimaryKey(id);
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 根据id删除角色信息
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/deleteSysRoleByid",method = RequestMethod.POST)
    public Object deleteSysRoleById(@RequestParam("id") String id) throws Exception {
        Object result = new ResponseResult<>();
        try{
            return sysRoleRest.deleteSysRoleById(id);

        }catch(Exception e) {
            log.error(e.getMessage(),e);
            return result;
        }
    }

    /**
     * 查询角色列表
     * @param obj
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/querySysRoleList",method = RequestMethod.POST)
    public Object querySysRoleList(@RequestBody SysRoleParam obj) throws Exception {
        Object result = new ResponseResult<>();
        try{
            return sysRoleRest.querySysRoleList(obj);

        }catch(Exception e) {
            log.error(e.getMessage(),e);
            return result;
        }

    }

    @RequestMapping(value="/saveRoleUser",method = RequestMethod.POST)
    public ResponseResult<SysRoleUserDTO> saveRoleUser(@RequestBody SysRoleUserDTO vo) throws Exception {
        ResponseResult<SysRoleUserDTO> result = new ResponseResult<>();
        try{
            if(vo!= null && vo.getId()>0) {
                vo.setTargetType("user");
                vo.setValidateState("1");
                result = sysRoleUserRest.update(vo);
            }else {
                vo.setTargetType("user");
                vo.setValidateState("1");
                result = sysRoleUserRest.create(vo);
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    @ApiOperation(value="查询与用户角色列表",notes = "查询结果:SUCCESS:200,FAILED:500",httpMethod = "POST")
    @RequestMapping(value="/queryRoleUserList",method = RequestMethod.POST)
    public Object queryRoleUserList(@RequestBody SysRoleUserParam obj) throws Exception {
        Object result = new ResponseResult<>();
        try{

            return sysRoleUserRest.querySysRoleUserList(obj);
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            return result;
        }

    }

    @RequestMapping(value="/deleteSysRoleUserById",method = RequestMethod.POST)
    public ResponseResult<Object> deleteSysRoleUserById(@RequestParam("id") String id) throws Exception {
        ResponseResult<Object> result = new ResponseResult<>();
        try{

            return sysRoleUserRest.deleteSysRoleUserById(id);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

}
